﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sinacor.UI.Application.Settings.SmartClient;
using System.Threading;
using Sinacor.UI.Application.Menu.SmartClient;
using Sinacor.Infra.UI.Session;
using Sinacor.Infra.UI.Authentication;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using System.Data;
using Sinacor.Infra.UI.Security.Licensing;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Configuration;

namespace Sinacor.Infra.InputTester.View.SmartClient
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        ApplicationSettings _settings;
        private const int DEFAULT_COMPANY_INDEX = 0;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _settings = ApplicationSettings.GetInstance();
            _settings.BeginInit();

            button1_Click(sender, e);

            //LoadCompanies();
        }

        private void LoadCompanies()
        {
            List<Company> companies = new List<Company>();
            OracleDataBase db = new OracleDataBase();
            DbCommand cmd = db.GetSqlStringCommand("select cd_empresa,nm_empresa from tgeempr");

            using (IDataReader reader = db.ExecuteReader(cmd))
            {
                while (reader.Read())
                {
                    Company company = new Company();
                    company.Id = reader.GetValue<Int32>("cd_empresa");
                    company.Name = reader.GetValue<string>("nm_empresa");
                    companies.Add(company);
                }
            }

            comboBox1.SelectedValuePath = "Id";
            comboBox1.DisplayMemberPath = "Name";            
            comboBox1.ItemsSource = companies;

            comboBox1.SelectedIndex = DEFAULT_COMPANY_INDEX;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            SessionManager sm = SessionManager.GetInstance();

            User user = new User();
            textBox1.Text = ConfigurationManager.AppSettings["user"].ToString();
            textBox2.Text = ConfigurationManager.AppSettings["pwd"].ToString(); 
            user.LoginName = textBox1.Text;
            user.Password = textBox2.Text;

            Company company = new Company();
            company.Id = 24;
            company.Name = "NOVO SINADES-(CCO) CORRENTE";

            sm.CreateSession(user, company);

            try
            {
                LicensingManager _licensingManager = LicensingManager.GetInstance();

            }
            catch (Exception lcex)
            {
                // Se der problema ao carregar o licenciamento
                // Apresenta uma mensagem de erro ao usuário
                string resourceKey = "Common.ExceptionMessage.LicenseFailed";
                string message = Application.Current.TryFindResource(resourceKey).ToString();
                SinacorMessageBox.Show(message, SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Error);
                App.Current.Shutdown();
            }

            //AlternativeMenu menu = new AlternativeMenu();
            //menu.Show();
            //this.Close();

            InputTesterView inputTesterView = new InputTesterView();
            inputTesterView.Show();
            this.Close();

        }
    }


}
